package cn.iocoder.yudao.module.biz.controller.admin.dangerTicket;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.dangerTicket.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.dangerTicket.BizDangerTicketDO;
import cn.iocoder.yudao.module.biz.convert.dangerTicket.BizDangerTicketConvert;
import cn.iocoder.yudao.module.biz.service.dangerTicket.BizDangerTicketService;

@Api(tags = "管理后台 - 危险作业票")
@RestController
@RequestMapping("/biz/danger-ticket")
@Validated
public class BizDangerTicketController {

    @Resource
    private BizDangerTicketService dangerTicketService;

    @PostMapping("/create")
    @ApiOperation("创建危险作业票")
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:create')")
    public CommonResult<String> createDangerTicket(@Valid @RequestBody BizDangerTicketCreateReqVO createReqVO) {
        return success(dangerTicketService.createDangerTicket(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新危险作业票")
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:update')")
    public CommonResult<Boolean> updateDangerTicket(@Valid @RequestBody BizDangerTicketUpdateReqVO updateReqVO) {
        dangerTicketService.updateDangerTicket(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除危险作业票")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:delete')")
    public CommonResult<Boolean> deleteDangerTicket(@RequestParam("id") String id) {
        dangerTicketService.deleteDangerTicket(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得危险作业票")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:query')")
    public CommonResult<BizDangerTicketRespVO> getDangerTicket(@RequestParam("id") String id) {
        BizDangerTicketDO dangerTicket = dangerTicketService.getDangerTicket(id);
        return success(BizDangerTicketConvert.INSTANCE.convert(dangerTicket));
    }

    @GetMapping("/list")
    @ApiOperation("获得危险作业票列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:query')")
    public CommonResult<List<BizDangerTicketRespVO>> getDangerTicketList(@RequestParam("ids") Collection<String> ids) {
        List<BizDangerTicketDO> list = dangerTicketService.getDangerTicketList(ids);
        return success(BizDangerTicketConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得危险作业票分页")
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:query')")
    public CommonResult<PageResult<BizDangerTicketRespVO>> getDangerTicketPage(@Valid BizDangerTicketPageReqVO pageVO) {
        PageResult<BizDangerTicketDO> pageResult = dangerTicketService.getDangerTicketPage(pageVO);
        return success(BizDangerTicketConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出危险作业票 Excel")
    @PreAuthorize("@ss.hasPermission('biz:danger-ticket:export')")
    @OperateLog(type = EXPORT)
    public void exportDangerTicketExcel(@Valid BizDangerTicketExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizDangerTicketDO> list = dangerTicketService.getDangerTicketList(exportReqVO);
        // 导出 Excel
        List<BizDangerTicketExcelVO> datas = BizDangerTicketConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "危险作业票.xls", "数据", BizDangerTicketExcelVO.class, datas);
    }

}
